import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
import qs from 'qs'

const baseUrlScheme = CommonConfig.schemaUrl + '/v1'
const planUrl = CommonConfig.planningUrl + '/v1'
const planningUrl = CommonConfig.planningUrl
const baseUrlTpm = CommonConfig.tpmUrl + '/v1'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

// 获取生产组织架构-树形选择器结构
export function getOrganize () {
  const newUrl = baseUrlScheme + '/scheme_tree'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 通过所在位置信息获取工位信息
export function getStation (level, schemeId) {
  const newUrl = baseUrlScheme + '/workcenter_no_page'

  let reqData = {
    params: {
      level,
      schemeId
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取设备台帐列表数据
export function getEquipmentAccount (deviceType, keyword, model, pageNum, pageSize, producer,
  searchBy, statusCode, workCenterId, schemeId, schemeLevel) {
  const newUrl = `${baseUrlTpm}/devices`

  let query = {
    params: {
      deviceType,
      keyword: keyword || undefined,
      model,
      pageNum,
      pageSize,
      producer,
      searchBy: searchBy || undefined,
      statusCode,
      workCenterId: workCenterId || undefined,
      schemeId: schemeId || undefined,
      schemeLevel: schemeLevel || undefined
    }
  }

  return axiosInstance.get(newUrl, query).then((res) => {
    return Promise.resolve(res)
  })
}

// 导出Excel
export function downloadDeviceExcel (data) {
  const url = `${baseUrlTpm}/devices/excel:export`

  return axiosInstance.get(url, { params: data }).then(resp => {
    return Promise.resolve(resp)
  })
}

// 获取设备厂家数据
export function getManufacturer () {
  const newUrl = `${baseUrlTpm}/devices/producers`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 通过设备厂家value获取
export function getModel (producer) {
  const newUrl = `${baseUrlTpm}/devices/models`

  let query = {
    params: {
      producer
    }
  }

  return axiosInstance.get(newUrl, query).then((res) => {
    return Promise.resolve(res)
  })
}

// 封禁设备
export function changeEquipmentStatus (deviceId, statusCode) {
  const newUrl = `${baseUrlTpm}/devices/${deviceId}`

  let data = {
    statusCode
  }

  return axiosInstance.patch(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

// 删除单个设备
export function deleteMachine (deviceId) {
  const newUrl = `${baseUrlTpm}/devices/${deviceId}`

  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 批量删除
export function batchDeletion (ids) {
  const newUrl = `${baseUrlTpm}/devices`

  let config = {
    params: {
      ids
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }

  return axiosInstance.delete(newUrl, config).then(resp => {
    return Promise.resolve(resp)
  })
}

// 新增设备
export function addEquipment (query) {
  const url = `${baseUrlTpm}/devices`

  return axiosInstance.post(url, query).then(resp => {
    return Promise.resolve(resp)
  })
}
// 获取设备信息
export function getDevices (id) {
  const url = `${baseUrlTpm}/devices/${id}`

  return axiosInstance.get(url).then(resp => {
    return Promise.resolve(resp)
  })
}

// 编辑设备
export function EditEquipment (query, id) {
  const url = `${baseUrlTpm}/devices/${id}`

  let params = {
    ...query
  }

  return axiosInstance.put(url, params).then(resp => {
    return Promise.resolve(resp)
  })
}

// 上传设备图片
export function uploadEquipmentPhoto (deviceId, photoUrls) {
  const newUrl = `${baseUrlTpm}/devices/${deviceId}`

  let data = {
    photoUrls
  }

  return axiosInstance.patch(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

// 上传设备档案
export function uploadEquipmentDocument (deviceId, documents) {
  const newUrl = `${baseUrlTpm}/devices/${deviceId}`

  let data = {
    documents
  }

  return axiosInstance.patch(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

// 列举工单
export function getWorkOrders (paramsData) {
  const newUrl = `${planningUrl}/v4/work_orders`
  const params = {
    ...paramsData,
    isSuspend: false,
    statusCodeList: [0, 3, 4]
  }
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res.data)
  })
}

/**
 * 获取质检单列表
 * keyword 关键字
 * pageNum 页码（从 1 起计）
 * pageSize 每页条数
 * searchBy 搜索依据 [workOrderNum]
 * showFlag 是否显示已关闭质检单 [true/false]
 * sortBy 排序依据 [workOrderNum/createTime/status]
 * value 排序值 [asc/desc]
 **/
export function getQualityOrders (query) {
  const params = {
    ...query,
    isPersonalComputer: true,
    status: 0
  }
  const newUrl = planUrl + '/quality_control_orders:list'
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res.data))
  })
}
// 列举工单
export function getRepairList (params) {
  const newUrl = `${baseUrlTpm}/device_repair/list`
  let reqData = {
    params: {
      ...params,
      record: false,
      status: [3, 2, 1],
      listType: 1
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}

// 列举保养计划
export function getScheduleList (params) {
  const newUrl = `${baseUrlTpm}/maintenance_schedule:page`
  let reqData = {
    params: {
      ...params,
      enableFlag: 1
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}
// 列举保养工单
export function getMaintenaceOrders (params) {
  const newUrl = `${baseUrlTpm}/maintenance_orders`
  let reqData = {
    params: {
      ...params,
      maintenanceStatus: '1,2',
      type: 'pc'
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res.data)
  })
}
// GET  /v1/device_index_by_workshop_id/{workshopId}获取车间设备排序
export function getDeviceByWorkshopId (workshopId) {
  const newUrl = `${baseUrlTpm}/device_index_by_workshop_id/${workshopId}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res.data)
  })
}
// POST  /v1/device_index设置设备排序
export function setDeviceIndex (params) {
  const newUrl = baseUrlTpm + '/device_index'
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res.data))
  })
}
// GET
// /v1/device_index_by_schema
// 获取车间设备排序(这里只会返回该组织下的设备,子组织下的设备不返回)

export function getDeviceBySchema (params) {
  const newUrl = `${baseUrlTpm}/device_index_by_schema`
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res.data)
  })
}
// /v1/device_index_schema
// 设置设备排序
export function setDeviceBySchema (params) {
  const newUrl = `${baseUrlTpm}/device_index_schema`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res.data)
  })
}
